package cn.hnbjx.prepaid.service;

import cn.hnbjx.prepaid.dao.FileDao;
import cn.hnbjx.prepaid.pojo.File;
import cn.hnbjx.prepaid.utils.FTPUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;


import javax.activation.MimetypesFileTypeMap;
import java.io.IOException;

@Service
public class UploadService {
    @Autowired
    private FTPUtil fTPUtil;
    @Autowired
    private FileDao fileDao;
    public String uploadImg(MultipartFile file) throws IOException {
        String hash = DigestUtils.md5Hex(file.getBytes());
        String image = fTPUtil.setImage(file);
            String type = new MimetypesFileTypeMap().getContentType(new java.io.File(image));
            File byFileHash = fileDao.findByFileHash(hash);
           if (null != byFileHash) {
                return byFileHash.getUrl();
            }
            File file1 = new File();
            file1.setType("image");
            file1.setUrl(image);
            file1.setHash(hash);
            fileDao.addFile(file1);
        return image;
    }
}
